﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace RpgEditor
{
    class MyIO
    {
              public static bool isDigitOnly(string s)
            {
                bool isDigitflag = true; // 检查是否全部是数字
                for (int j = 0; j < s.Length; j++)
                {
                    if (!Char.IsDigit(s, j)) { isDigitflag = false; break; }
                    string s1 = s[j].ToString();
                    if (s1.Equals(".")) { isDigitflag = false; break; }
                }
                return isDigitflag;
            }
            public static int swapInt32(int v)
            {
                byte[] b = BitConverter.GetBytes(v);
                byte a;
                a = b[0]; b[0] = b[3]; b[3] = a;
                a = b[1]; b[1] = b[2]; b[2] = a;
                return BitConverter.ToInt32(b, 0);
            }

            public static double swapDouble(double v)
            {
                byte[] b = BitConverter.GetBytes(v);
                byte a;
                a = b[0]; b[0] = b[7]; b[7] = a;
                a = b[1]; b[1] = b[6]; b[6] = a;
                a = b[2]; b[2] = b[5]; b[5] = a;
                a = b[3]; b[3] = b[4]; b[4] = a;
                return BitConverter.ToDouble(b, 0);
            }
        public static int writeInt64(BinaryWriter fs, long v)
            {
                byte[] b = BitConverter.GetBytes(v);
                byte a;
                a = b[0]; b[0] = b[7]; b[7] = a;
                a = b[1]; b[1] = b[6]; b[6] = a;
                a = b[2]; b[2] = b[5]; b[5] = a;
                a = b[3]; b[3] = b[4]; b[4] = a;
                fs.Write(b, 0, 8);
                return 8;
            }
            public static int writeInt64(Stream fs, long v)
            {
                byte[] b = BitConverter.GetBytes(v);
                byte a;
                a = b[0]; b[0] = b[7]; b[7] = a;
                a = b[1]; b[1] = b[6]; b[6] = a;
                a = b[2]; b[2] = b[5]; b[5] = a;
                a = b[3]; b[3] = b[4]; b[4] = a;
                fs.Write(b, 0, 8);
                return 8;
            }

            public static int writeInt32(BinaryWriter bw, int v)
            {
                byte[] b = new byte[4];
                b[3] = (byte)v;
                b[2] = (byte)(v >> 8);
                b[1] = (byte)(v >> 16);
                b[0] = (byte)(v >> 24);
                bw.Write(b);
                return 4;
            }
            public static int writeInt32(FileStream fs, int v)
            {
                byte[] b = new byte[4];
                b[3] = (byte)v;
                b[2] = (byte)(v >> 8);
                b[1] = (byte)(v >> 16);
                b[0] = (byte)(v >> 24);
                fs.Write(b, 0, 4);
                return 4;
            }
            public static int writeInt32(Stream fs, int v)
            {
                byte[] b = new byte[4];
                b[3] = (byte)v;
                b[2] = (byte)(v >> 8);
                b[1] = (byte)(v >> 16);
                b[0] = (byte)(v >> 24);
                fs.Write(b, 0, 4);
                return 4;
            }

            public static int readInt32(BinaryReader br)
            {
                int v = br.ReadInt32();
                byte[] b = BitConverter.GetBytes(v);
                byte a;
                a = b[0]; b[0] = b[3]; b[3] = a;
                a = b[1]; b[1] = b[2]; b[2] = a;
                return BitConverter.ToInt32(b, 0);
            }
            public static int readInt32(FileStream fs)
            {
                byte[] b = new byte[4];
                fs.Read(b, 0, 4);
                byte a;
                a = b[0]; b[0] = b[3]; b[3] = a;
                a = b[1]; b[1] = b[2]; b[2] = a;
                return BitConverter.ToInt32(b, 0);
            }
            public static int readInt32(Stream fs)
            {
                byte[] b = new byte[4];
                fs.Read(b, 0, 4);
                byte a;
                a = b[0]; b[0] = b[3]; b[3] = a;
                a = b[1]; b[1] = b[2]; b[2] = a;
                return BitConverter.ToInt32(b, 0);
            }
            //======================zhangac 2008-9-19===============================
            public static int writeInt16(BinaryWriter bw, short v)
            {
                byte[] b = new byte[2];
                b[1] = (byte)v;
                b[0] = (byte)(v >> 8);
                bw.Write(b);
                return 2;
            }
            public static int writeInt16(FileStream fs, short v)
            {
                byte[] b = new byte[2];
                b[1] = (byte)v;
                b[0] = (byte)(v >> 8);
                fs.Write(b, 0, 2);
                return 2;
            }
            public static int writeInt16(Stream fs, short v)
            {
                byte[] b = new byte[2];
                b[1] = (byte)v;
                b[0] = (byte)(v >> 8);
                fs.Write(b, 0, 2);
                return 2;
            }

            public static short readInt16(BinaryReader br)
            {
                short v = br.ReadInt16();
                byte[] b = BitConverter.GetBytes(v);
                byte a;
                a = b[0]; b[0] = b[1]; b[1] = a;
                return BitConverter.ToInt16(b, 0);
            }
            public static short readInt16(FileStream fs)
            {
                byte[] b = new byte[2];
                fs.Read(b, 0, 2);
                byte a;
                a = b[0]; b[0] = b[1]; b[1] = a;
                return BitConverter.ToInt16(b, 0);
            }
            public static short readInt16(Stream fs)
            {
                byte[] b = new byte[2];
                fs.Read(b, 0, 2);
                byte a;
                a = b[0]; b[0] = b[1]; b[1] = a;
                return BitConverter.ToInt16(b, 0);
            }


        public static int writeByte(BinaryWriter bw, SByte b)
            {
                bw.Write((Byte)b);
                return 1;
            }
            //public static int writeByte(FileStream fs, byte b)
            //{
            //    fs.WriteByte(b);
            //    return 1;
            //}
            //public static int writeByte(Stream fs, byte b)
            //{
            //    fs.WriteByte(b);
            //    return 1;
            //}

            public static SByte readByte(BinaryReader br)
            {
                SByte b = (SByte)br.ReadByte();
                return b;
            }
        //public static SByte readByte(FileStream fs)
        //    {
        //        SByte b = (SByte)fs.ReadByte();
        //        return b;
        //    }
        //public static SByte readByte(Stream fs)
        //    {
        //        SByte b = (SByte)fs.ReadByte();
        //        return b;
        //    }
            //======================zhangac 2008-9-19===============================
        public static long readInt64(BinaryReader fs)
            {
                byte[] b = new byte[8];
                fs.Read(b, 0, 8);
                byte a;
                a = b[0]; b[0] = b[7]; b[7] = a;
                a = b[1]; b[1] = b[6]; b[6] = a;
                a = b[2]; b[2] = b[5]; b[5] = a;
                a = b[3]; b[3] = b[4]; b[4] = a;
                return BitConverter.ToInt64(b, 0);
            }

            public static double readDouble(BinaryReader br)
            {
                double v = br.ReadDouble();
                byte[] b = BitConverter.GetBytes(v);
                byte a;
                a = b[0]; b[0] = b[7]; b[7] = a;
                a = b[1]; b[1] = b[6]; b[6] = a;
                a = b[2]; b[2] = b[5]; b[5] = a;
                a = b[3]; b[3] = b[4]; b[4] = a;
                return BitConverter.ToDouble(b, 0);
            }
            public static int writeDouble(BinaryWriter bw, double v)
            {
                byte[] b = BitConverter.GetBytes(v);
                if (b.Length == 8)
                {
                    byte a;
                    a = b[0]; b[0] = b[7]; b[7] = a;
                    a = b[1]; b[1] = b[6]; b[6] = a;
                    a = b[2]; b[2] = b[5]; b[5] = a;
                    a = b[3]; b[3] = b[4]; b[4] = a;
                    bw.Write(b);
                    return 8;
                }
                return -1;
            }
        public static void writeString(BinaryWriter bw, string value)
            {
                byte[] buffer = Encoding.UTF8.GetBytes(value);
                Write(bw,(ushort)buffer.Length);
                Write(bw,buffer);
            }

            /**输出8位无符号整形数组*/
        public static void Write(BinaryWriter bw, byte[] value)
            {
                bw.Write(value, 0, value.Length);
            }

            /**输出16位无符号的整型*/
            public static void Write(BinaryWriter bw,ushort value)
            {
                byte[] buffer = BitConverter.GetBytes(value);
                byte a;
                a = buffer[0]; buffer[0] = buffer[1]; buffer[1] = a;
                bw.Write(buffer);
            }
            public static string readString(BinaryReader br)
            {
               
                //int passwd = (int)br.BaseStream.Position;
                //br.ReadChar();
                //char len = br.ReadChar();
                //byte[] bin = new byte[len];
                //br.Read(bin, 0, bin.Length);
 
                //return UTF8Encoding.UTF8.GetString(bin);

                int length =MyIO.ReadUInt16(br);
                byte[] value = MyIO.ReadBytes(length, br);
                return UTF8Encoding.UTF8.GetString(value);
            }
            /**读取指定长度的字节数组*/
        public static byte[] ReadBytes(int length, BinaryReader br)
            {
                byte[] value = new byte[length];
                br.Read(value, 0, length);
                return value;
            }

        public static ushort ReadUInt16(BinaryReader br)
            {
                byte[] value = ReadBytes(2,br);
                byte a;
                a = value[0]; value[0] = value[1]; value[1] = a;
                return BitConverter.ToUInt16(value, 0);
            }
            //		public static void imgXorData(byte[] bin, int offset, int len)
            //		{
            //			if (bin == null || bin.Length < offset + len) return;
            //
            //			for(int i = 0; i < len; i++)
            //				bin[offset + i] ^= desKey[i % 8];
            //		}
 

            // 摘要:
            //    将4字节的整型转换成Byte数组
            //     张志强添加，2007-12-4
            public static byte[] Int32ToByteArr(int i)
            {
                //Console.WriteLine("Int32ToByteArr: " + i);
                byte[] i4 = new byte[4];
                i4[0] = (byte)(i >> 24 & 0xff);
                i4[1] = (byte)(i >> 16 & 0xff);
                i4[2] = (byte)(i >> 8 & 0xff);
                i4[3] = (byte)(i >> 0 & 0xff);
                return i4;
            }

            // 摘要:
            //    将4字节的整型转换成Byte数组
            //     张志强添加，2007-12-4
            // 参数:
            //    长度为4的byte数组
            public static int byteArrToInt32(byte[] i4)
            {
                int j = ((int)i4[0] << 24) + ((int)i4[1] << 16) + ((int)i4[2] << 8) + ((int)i4[3] << 0);
                //Console.WriteLine("byteArrToInt32: " + j);
                return j;
            }
        
    }
}
